package com.analyse.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.analyse.stock.constant.ApiConstant;
import com.analyse.stock.constant.StockConstant;
import com.analyse.stock.mapper.CountryInfoMapper;
import com.analyse.stock.model.CountryInfoModel;
import com.analyse.stock.service.ICountryInfo;
import com.analyse.stock.utils.HttpUtil;
import com.analyse.stock.vo.TickerVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CountryInfoService extends ServiceImpl<CountryInfoMapper, CountryInfoModel> implements ICountryInfo {

    @Override
    public Integer pullCountryList() {
        List<CountryInfoModel> stockCountryList = this.pullCountryList(1);
        List<CountryInfoModel> indexCountryList = this.pullCountryList(2);
        if(CollUtil.isNotEmpty(indexCountryList)){
            Map<String,CountryInfoModel> maps = indexCountryList.stream()
                    .collect(Collectors.toMap(CountryInfoModel::getCountryCode, Function.identity(),(v1,v2)->v1));
            for(CountryInfoModel model : stockCountryList){
                if(CollUtil.isEmpty(maps) || !maps.containsKey(model.getCountryCode())){
                    continue;
                }
                CountryInfoModel model1 = maps.get(model.getCountryCode());
                model.setTimezone(model1.getTimezone());
                model.setDelay(model1.getDelay());
                model.setNotes(model1.getNotes());
            }
        }
        super.saveBatch(stockCountryList);
        return stockCountryList.size();
    }

    private List<CountryInfoModel> pullCountryList(Integer sourceType) {
        //来源 1-stock,2-index
        String url = null;
        switch (sourceType){
            case 1:
                url = ApiConstant.STOCK_COUNTRY_URL;
                break;
            case 2:
                url = ApiConstant.INDEX_COUNTRY_URL;
                break;
            default:
                log.info("### 拉取国家/地区清单，sourceType不支持(1-stock,2-index)：{}",sourceType);
                return Lists.newArrayList();
        }
        String result = HttpUtil.sendGet(url,"*/*", StockConstant.UTF8,6000L);
        if(StringUtils.isBlank(result)){
            return Lists.newArrayList();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        if(jsonObject.getInteger("code") != 200 || Objects.isNull(jsonObject.getJSONArray("data"))){
            return Lists.newArrayList();
        }
        List<TickerVO> list = JSONObject.parseArray(jsonObject.getString("data"), TickerVO.class);
        return list.stream().map(CountryInfoModel::transCountryInfoModel).collect(Collectors.toList());
    }

    @Override
    public List<CountryInfoModel> getCountryByCodes(String countryCodes){
        if(StringUtils.isNotBlank(countryCodes)){
            LambdaUpdateWrapper<CountryInfoModel> wrapper = new LambdaUpdateWrapper<>();
            wrapper.in(CountryInfoModel::getCountryCode, Arrays.asList(countryCodes.split(StrUtil.COMMA)));
            return super.list(wrapper);
        }
        return super.list();
    }
}
